package reone.android.anytest.CameraTest;

import android.app.Activity;
import android.hardware.Camera;
import android.media.CamcorderProfile;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.view.SurfaceHolder;
import android.view.View;
import android.widget.Button;
import android.widget.MediaController;
import android.widget.VideoView;

import java.io.File;
import java.io.IOException;

import reone.android.anytest.Util.LogUtil;
import reone.android.anytest.R;

public class MainCameraActivity extends Activity implements SurfaceHolder.Callback{

    /** Called when the activity is first created. */


    private MediaRecorder record = null;
    private static final String OUTPUT_FILE = "/sdcard/videooutput.3gp";
    private static final String TAG = "RecordVideo";
    private VideoView vv = null;
    private Button startBtn = null;
    private Camera mCamera;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.actvity_camera_layout);

        startBtn = (Button)findViewById(R.id.Begin);
        Button endBtn = (Button)findViewById(R.id.Stop);
        Button playRecordingBtn = (Button)findViewById(R.id.Play);
        Button stpPlayingrecordingBtn = (Button)findViewById(R.id.StopPlaying);
        vv = (VideoView)findViewById(R.id.videoview);

        final SurfaceHolder holder = vv.getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        startBtn.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    beginRecording(holder);
                }catch(Exception e){
                    LogUtil.out(TAG, e.toString());
                    e.printStackTrace();
                }
            }});

        endBtn.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    stopRecording();
                }catch(Exception e){
                    LogUtil.out(TAG, e.toString());
                    e.printStackTrace();
                }
            }});

        playRecordingBtn.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    playRecording();
                }catch(Exception e){
                    LogUtil.out(TAG, e.toString());
                    e.printStackTrace();
                }
            }});

        stpPlayingrecordingBtn.setOnClickListener(new Button.OnClickListener(){

            public void onClick(View v) {
                // TODO Auto-generated method stub
                try{
                    stopPlayingRecording();
                }catch(Exception e){
                    LogUtil.out(TAG, e.toString());
                    e.printStackTrace();
                }
            }});
    }

    public void surfaceCreated(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        startBtn.setEnabled(true);
        LogUtil.out(TAG, "surfaceCreated");
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int width,
                               int height) {
        // TODO Auto-generated method stub
        LogUtil.out(TAG, "Width x Height = " + width + "x" + height);
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // TODO Auto-generated method stub
        LogUtil.out(TAG, "surfaceDestroyed");
    }

    protected void beginRecording(SurfaceHolder holder) throws Exception{
        // TODO Auto-generated method stub
        LogUtil.out(TAG, "beginRecording record = " + record);
        if(record != null){
            record.stop();
            record.release();
        }

        File outFile = new File(OUTPUT_FILE);
        if(outFile.exists()){
            outFile.delete();
        }

        try{
            initCamera(holder);
            record = new MediaRecorder();
            record.setCamera(mCamera);
            record.setVideoSource(MediaRecorder.VideoSource.CAMERA);
            record.setAudioSource(MediaRecorder.AudioSource.MIC);
            record.setOrientationHint(90);
            CamcorderProfile profile = CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH);
            record.setProfile(profile);
            record.setPreviewDisplay(holder.getSurface());
            record.setOutputFile(OUTPUT_FILE);
            record.prepare();
            record.start();
        }catch(Exception e){
            LogUtil.out(TAG, e.toString());
            e.printStackTrace();
        }
    }

    protected void stopRecording() {
        // TODO Auto-generated method stub
        LogUtil.out(TAG, " stopRecording record = " + record);
        if(record != null){
            record.stop();
            record.release();
            record = null;
        }
        freeCameraResource();
    }

    protected void playRecording() {
        // TODO Auto-generated method stub
        LogUtil.out(TAG, " playRecording");
        MediaController mc = new MediaController(this);
        vv.setMediaController(mc);
        vv.setVideoPath(OUTPUT_FILE);
        vv.start();
    }

    protected void stopPlayingRecording() throws Exception{
        LogUtil.out(TAG, " stopPlayingRecording ");
        // TODO Auto-generated method stub
        vv.stopPlayback();
    }

    private void initCamera(SurfaceHolder holder) throws IOException {
        if (mCamera != null) {
            freeCameraResource();
        }
        try {
            mCamera = Camera.open();
        } catch (Exception e) {
            e.printStackTrace();
            freeCameraResource();
        }
        if (mCamera == null)
            return;

        if (mCamera != null) {
            Camera.Parameters params = mCamera.getParameters();
            params.set("orientation", "portrait");
            mCamera.setParameters(params);
        }
        mCamera.setDisplayOrientation(90);
        mCamera.setPreviewDisplay(holder);
        mCamera.startPreview();
        mCamera.unlock();
    }
    private void freeCameraResource() {
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.lock();
            mCamera.release();
            mCamera = null;
        }
    }
}
